package com.shawn.spring.boot.jdbc.run;

import com.shawn.spring.boot.jdbc.entity.User;
import com.shawn.spring.boot.jdbc.service.UserService;
import java.util.Arrays;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author shawn
 * @since 2020/8/28
 */
@Component
@Slf4j
public class JdbcTemplateApplicationRunner implements ApplicationRunner {

  @Autowired private UserService service;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    log.info("test insert");
    testInsert();
    log.info("test batch insert");
    testBatchInsert();
    log.info("test insert return key");
    int i = testInsertReturnKey();
    log.info("test find all");
    testFindAll();
    log.info("test find by id");
    testFindById(i);
    log.info("test find by condition");
    testFindByCondition();
    log.info("before update");
    testFindById(i);
    Thread.sleep(1000);
    log.info("action updating");
    testUpdate(i);
    log.info("after update");
    testFindById(i);
    log.info("test delete");
    testDelete(i);
    log.info("after delete");
    testFindById(i);
    log.info(service.count() + "");
    testFindById(100);
  }

  private void testInsert() {
    Date now = new Date();
    User user =
        User.builder()
            .username("shawn")
            .password("shawn")
            .nickname("shawn")
            .email("1111@qq.com")
            .createTime(now)
            .updateTime(now)
            .isDeleted(false)
            .build();
    service.insert(user);
  }

  private void testBatchInsert() throws InterruptedException {
    Date now = new Date();
    User jack =
        User.builder()
            .username("jack")
            .password("jack")
            .nickname("jack")
            .email("2222@qq.com")
            .createTime(now)
            .updateTime(now)
            .isDeleted(false)
            .build();
    service.batchInsertWithJbdcTemplate(Arrays.asList(jack));
    Thread.sleep(1000);
    User bill =
        User.builder()
            .username("bill")
            .password("bill")
            .nickname("bill")
            .email("3333@qq.com")
            .createTime(new Date())
            .updateTime(new Date())
            .isDeleted(false)
            .build();
    Thread.sleep(1000);
    User john =
        User.builder()
            .username("john")
            .password("john")
            .nickname("john")
            .email("4444@qq.com")
            .createTime(new Date())
            .updateTime(new Date())
            .isDeleted(false)
            .build();
    service.batchInsertWithBatchPreparedStatementSetter(Arrays.asList(bill, john));
    Thread.sleep(1000);
    User bob =
        User.builder()
            .username("bob")
            .password("bob")
            .nickname("bob")
            .email("5555@qq.com")
            .createTime(new Date())
            .updateTime(new Date())
            .build();
    Thread.sleep(1000);
    User jackson =
        User.builder()
            .username("jackson")
            .password("jackson")
            .nickname("jackson")
            .email("6666@qq.com")
            .createTime(new Date())
            .updateTime(new Date())
            .build();
    service.batchInsertWithNamedParams(Arrays.asList(bob, jackson));
  }

  private int testInsertReturnKey() {
    User johnson =
        User.builder()
            .username("johnson")
            .password("johnson")
            .nickname("johnson")
            .email("7777@qq.com")
            .createTime(new Date())
            .updateTime(new Date())
            .isDeleted(false)
            .build();
    int i = service.insertReturnKey(johnson);
    log.info("id {}", i);
    return i;
  }

  private void testFindAll() {
    service.findAll().forEach(user -> log.info(user.toString()));
  }

  private User testFindById(int id) {
    User user = service.findById(id);
    log.info(user == null ? null : user.toString());
    return user;
  }

  private void testFindByCondition() {
    service
        .findByCondition(User.builder().username("john").password("john").nickname("john").build())
        .forEach(user -> log.info(user.toString()));
  }

  private void testUpdate(int id) {
    User user = testFindById(id);
    user.setUpdateTime(new Date());
    user.setPassword("newPass");
    service.modify(user);
  }

  private void testDelete(int id) {
    service.remove(id);
  }
}
